package org.bot.server.config;

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 沧月
 * @create 0020 2023/9/20 10:28
 * @description <description class purpose>
 */
@Configuration
public class DruidConfig {

    @Value("${druid-admin.username}")
    private String username;

    @Value("${druid-admin.password}")
    private String password;

    @Value("${druid-admin.reset-enable}")
    private String resetEnable;

    @Value("${druid-admin.allow}")
    private String allow;

    @Value("${druid-admin.exclusions}")
    private String exclusions;


    @Bean
    public ServletRegistrationBean statViewServle(){
        ServletRegistrationBean servletRegistrationBean=new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
        //IP白名单
        servletRegistrationBean.addInitParameter("allow",allow);
        //控制台用户
        servletRegistrationBean.addInitParameter("loginUsername",username);
        servletRegistrationBean.addInitParameter("loginPassword",password);
        //是否能够重置数据
        servletRegistrationBean.addInitParameter("resetEnable",resetEnable);
        return servletRegistrationBean;
    }
    @Bean
    public FilterRegistrationBean statFilter(){
        FilterRegistrationBean filterRegistrationBean=new FilterRegistrationBean(new WebStatFilter());
        //添加过滤规则
        filterRegistrationBean.addInitParameter("exclusions",exclusions);
        return filterRegistrationBean;
    }
}
